package 字符串算法;

public class 验证ip地址 {
    public String solve (String IP) {
        return isvalue4(IP)?"IPv4":(isvalue6(IP)?"IPv6":"Neither");

    }
    public boolean isvalue4(String IP){
        String[] split = IP.split("\\.", -1);
        if (split.length!=4){
            return false;
        }
        for (String str : split) {
            if (str.length()>1&&str.startsWith("0")){
                return false;
            }
            try{
                int val = Integer.parseInt(str);
                if (!(val>=0||val<=255)){
                    return false;
                }
            }catch (NumberFormatException numberFormatException){
                return false;
            }
        }
        return true;
    }
    public boolean isvalue6(String IP){
        String[] split = IP.split("\\:", -1);
        if (split.length!=8) return false;
        for (String str : split) {
            if (str.length()>4||str.length() == 0){
                return false;
            }try {
                //转化为16进制
                int val = Integer.parseInt(str,16);
            }catch (NumberFormatException numberFormatException){
                return false;
            }

        }
        return true;
    }
}
